<template>
  <div class="departments-container">
    <div class="app-container">

      <el-card v-loading="loading" class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tool :node-data="company" :is-root="true" @add-depts="addDept" />
        <!--
          props ：配置选项 默认为 label children
          default-expand-all 是否默认展开所有  默认为false 不展开
          完整写法  :default-expand-all="true"
          简略写法  default-expand-all
         -->
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <template #default="{data}">
            <tree-tool :node-data="data" @add-depts="addDept" @edit-depts="editDept" @del-depart="getCompanyDepartment" />
          </template>
        </el-tree>
      </el-card>
    </div>
    <!-- <add-dept :depts="depts" :node-data="nodeData" :dialog-visible="dialogVisible" @add-depts="getCompanyDepartment" @closeDialog="closeDialog" /> -->
    <add-dept ref="addDeptRef" :depts="depts" :node-data="nodeData" :dialog-visible.sync="dialogVisible" @add-depts="getCompanyDepartment" @closeDialog="closeDialog" />
  </div>
</template>

<script>
import { tranListToTreeData } from '@/utils'
import treeTool from './compoments/tree-tool.vue'
import { reqGetCompanyDepartment } from '@/api/departments'
import AddDept from './compoments/add-dept.vue'
export default {
  name: 'Departments',
  components: { treeTool, AddDept },
  // 祖孙通讯
  // inject: ['qq'],
  data() {
    return {
      loading: false,
      dialogVisible: false,
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' },
      departs: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      nodeData: {},
      depts: []
    }
  },
  created() {
    this.getCompanyDepartment()
    // console.log(11, this.qq)
  },
  methods: {
    async getCompanyDepartment() {
      this.loading = true
      const { data: { depts, companyName }} = await reqGetCompanyDepartment()
      console.log(JSON.parse(JSON.stringify(depts)))
      this.departs = tranListToTreeData(depts, '')
      this.depts = depts
      this.company.name = companyName
      this.loading = false
    },
    closeDialog(flag) {
      this.dialogVisible = flag
    },
    addDept(nodeData) {
      this.dialogVisible = true
      this.nodeData = nodeData
    },
    editDept(nodeData) {
      this.dialogVisible = true
      this.nodeData = nodeData
      // 之后的回显需求，需要调用请求，会拿到更详细的数据，而不用nodeData
      // 调用获取简单员工列表数据 - 如何调用另外个组件的方法
      // 大致参考的方案 就类似我们form实例的方法调用 this.$refs.xxx.validate
      // 通过设置ref属性，可以拿到对应组件的实例，这样实例上的方法数据都能拿到
      // console.log(this.$refs.addDeptRef)
      this.$refs.addDeptRef.getEmployeeSimple()
      this.$refs.addDeptRef.getDepartDetail(nodeData.id)
    }
  }
}
</script>

<style scoped>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
}
</style>
